﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
    <link rel="stylesheet" type="text/css" href="/admin/EasyUi/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="/admin/EasyUi/themes/icon.css" />

    <script type="text/javascript" src="/admin/Common/Scripts/jquery-1.12.0.min.js"></script>
    <script type="text/javascript" src="/admin/EasyUi/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/admin/EasyUi/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/admin/Common/Scripts/Utils.js"></script>
</head>
<body>
    <div>
        <style type="text/css">
            .box {
                border: 1px solid #bbb;
                background: #eee;
                margin-top: 0;
                margin-bottom: 10px;
                padding: 5px;
                line-height: 36px;
            }

                .box .title {
                    background: #eee;
                    height: 36px;
                    font-weight: normal;
                }

                    .box .title .tip {
                        font-weight: bold;
                        margin-right: 8px;
                        padding: 0 3px;
                        cursor: pointer;
                    }

                    .box .title .handle {
                        float: right;
                        margin-right: 10px;
                    }

                .box .content {
                    border-top: 1px solid #bbb;
                    padding-left: 30px;
                    display: block;
                }
        </style>

        @{
            var MenuObj = ViewBag.MenuObj;
            var menuType = ViewBag.menuType;
            var editType = MenuObj.AdminMenuId > 0 ? "操作类型：<strong>修改</strong>" : "操作类型：<strong>新增</strong>";
            var num = 1;
            var relUrlNum = 1;
           
        }


        <script type="text/javascript">
            @*$(function () {
                $("*").click(function () {
                    if (parent)
                        parent.dyniframesize();
                })
            });
            var msg = "@message";
            if (msg) {
                alert(msg);
            }*@
        </script>

        <div id="p" class="easyui-panel" data-options="noheader:true" style="width:100%; text-align:center;padding:20px;">
            <span style="color:#f00;">页面id命名规范：按钮管理中请使用btn_开头，条件管理中请使用filter_开头，全局权限中请使用public_开头（只要是public_开头的都会被认为是全局权限）</span>
        </div>

        @if (MenuObj.AdminMenuId > 0)
        {
            <div class="easyui-panel" data-options="height:'30px'">
                <a id="btn_reload" href="javascript: location.href='Edit?action=add&parentId=0&adminMenuId=0';" class="easyui-linkbutton" style="width:100px;height:30px" data-options="iconCls:'icon-add'">添加顶级类</a>
                <a id="btnDelete" href="javascript: DetMenuFun(@(MenuObj.AdminMenuId),'@(MenuObj.IsParent)');" class="easyui-linkbutton" style="width:100px;height:30px" data-options="iconCls:'icon-remove'">删除该分类</a>

                <a id="addSub" href="javascript: location.href='Edit?action=add&parentId=@(MenuObj.AdminMenuId)&adminMenuId=0';" class="easyui-linkbutton" style="width:100px;height:30px" data-options="iconCls:'icon-add'">添加子分类</a>
            </div>
        }
        <form id="editform" action="DoEdit" method="post">
            <div class="easyui-panel" title="@editType" data-options="fit:false">


                <table class="easyui-datagrid" data-options="showHeader:false,singleSelect:true,checkOnSelect:false,striped:true" style="width:100%;">
                    <thead>
                        <tr>
                            <th data-options="field:'code',align:'right'" style="width:30%">标题</th>
                            <th data-options="field:'name'" style="width:70%">内容</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td style="background-color:#fff; width:220px; text-align:center;"></td>
                            <td style=" background-color:#fff; text-align:right;"></td>
                        </tr>
                        <tr>
                            <td style="text-align:right;">菜单名称：</td>
                            <td style=" background-color:#fff;">
                                <input id="menuName" name="menuName" type="text" value="@(MenuObj.MenuName)" /><br />
                            </td>
                        </tr>
                        <tr>
                            <td style="background-color:#fff;width:220px; text-align:right;"></td>
                            <td style=" background-color:#fff;">
                                @{
                                    var showvalue=MenuObj.ShowFlag?1:0;
                                    var stopvalue=MenuObj.StopFlag?1:0;
                                }
                                <input type="hidden" name="showFlag" id="showFlagHD" value="@showvalue"   />
                                <input type="hidden" name="stopFlag" id="stopFlagHD" value="@stopvalue" />
                                <input id="showFlag" type="checkbox" value="@showvalue" checked="@(MenuObj.ShowFlag)" onclick="setValue(1)" />显示
                                <input id="stopFlag" type="checkbox" value="@stopvalue" checked="@(MenuObj.StopFlag)" onclick="setValue(0)" />停用
                            </td>
                        </tr>
                        <tr>
                            <td style="background-color:#fff;text-align:right;">上级菜单：</td>
                            <td style=" background-color:#fff;">
                                <input id="parentId" name="parentId" type="hidden" value="@(MenuObj.ParentId)" />
                                @(MenuObj.ParentName) （ID：@(MenuObj.ParentId))

                            </td>
                        </tr>
                        <tr>
                            <td style="background-color:#fff;text-align:right;">菜单路径：</td>
                            <td style=" background-color:#fff;">
                                <input id="MenuUrl" name="MenuUrl" type="text" value="@(MenuObj.Url)" style="width:70%;" />
                                <div style="color:Red; display:none;" id="urlEorrorMsg">重复的路径，使用该路径的菜单为“<span id="repeatUrlMenuName"></span>”</div>
                            </td>
                        </tr>
                        <tr>
                            <td style=" background-color:#fff; text-align:right;">排序ID：</td>
                            <td style=" background-color:#fff;">
                                <input id="sortOrder" name="sortOrder" type="text" value="@(MenuObj.SortOrder)" />
                            </td>
                        </tr>


                    </tbody>
                </table>


            </div>
            <div class="easyui-panel" title="菜单关联的路径" data-options="">

                <div style="margin-left:30px;">
                    <div id="UrlDiv">
                        @if (ViewBag.RelevanceUrlList != null)
                        {
                            foreach (var item in ViewBag.RelevanceUrlList)
                            {
                            <p id="UrlSpan_@(relUrlNum)">
                                菜单关联的路径<em id="em_@(relUrlNum)">@(relUrlNum)</em>：　Id：<input type="text" id="RelevanceUrl_@(relUrlNum)" name="RelevanceUrl_@(relUrlNum)" value="@(item)" size="50" />
                                <a href="javascript:void(0);" onclick="deleteRelevanceUrl(@(relUrlNum++));">删除</a>
                            </p>
                            }
                        }
                        <p id="UrlSpan_@(relUrlNum)">
                            菜单关联的路径<em id="em_@(relUrlNum)">@(relUrlNum)</em>：　Id：<input type="text" id="RelevanceUrl_@(relUrlNum)" name="RelevanceUrl_@(relUrlNum)" value="" size="50" />
                            <a href="javascript:void(0);" onclick="deleteRelevanceUrl(@(relUrlNum++));">删除</a>
                        </p>
                    </div>
                    <br /><a href="javascript:void(0);" onclick="AddNextUrl();">继续增加</a>
                </div>

            </div>
            <div class="easyui-panel" title="按钮管理（必填项未填的将不会保存） 同一菜单下的页面ID不能重复" data-options="">


                <div id="btnList" style="">
                    @foreach (var item in ViewBag.BtnList)
                    {
                        <dl class="box">
                            <dt class="title">
                                <input type="hidden" name="btnId" value="@item.BtnId" />
                                <input type="hidden" name="btnType" value="0" />
                                <input type="hidden" name="relList" value="@item.RelUrl" />
                                <span class="tip">－</span>
                                <span>
                                    *按钮名称：<input type="text" name="btnName" value="@item.BtnName" maxlength="50" style="width:150px; margin-right:10px;" />
                                    *页面ID：<input type="text" name="btnClientId" value="@item.ClientId" maxlength="128" style="width:150px; margin-right:10px;" />
                                    排序：<input type="text" name="btnSortNo" value="@item.SortNo" maxlength="6" style="width:60px; margin-right:10px;" />
                                </span>
                                <span class="handle"><a class="del" href="javascript:;">删除按钮</a></span>
                            </dt>
                            <dd class="content">

                                @foreach (var url in item.RelList)
                                {
                                    <div>关联Url&nbsp;&nbsp;&nbsp;<i style="margin:0 10px 0 2px;">@(num++)</i><input type="text" name="RelUrl" style="width:70%; margin-right:10px;" value="@url" /><a class="del" href="javascript:;">删除</a></div>

                                }
                                <div>关联Url&nbsp;&nbsp;&nbsp;<i style="margin:0 10px 0 2px;">@(num++)</i><input type="text" name="RelUrl" style="width:70%; margin-right:10px;" /><a class="del" href="javascript:;">删除</a></div>
                                <div><a href="javascript:;" class="addUrl">添加关联Url</a></div>
                            </dd>
                        </dl>
                    }
                    <div style=""><a href="javascript:;" id="addBtn">添加按钮</a></div>
                </div>
                <div class="easyui-panel" title="搜索条件管理（必填项未填的将不会保存） 同一菜单下的页面ID不能重复" data-options="">

                    <div id="filterList">
                        @foreach (var item in ViewBag.FilterList)
                        {
                            <dl class="box">
                                <dt class="title">
                                    <input type="hidden" name="btnId" value="@item.BtnId" />
                                    <input type="hidden" name="btnType" value="1" />
                                    <input type="hidden" name="relList" value="" />
                                    <input type="hidden" name="RelUrl" value="" />
                                    <span>
                                        *名称或说明：<input type="text" name="btnName" value="@item.BtnName" maxlength="50" style="width:150px; margin-right:10px;" />
                                        *页面ID：<input type="text" name="btnClientId" value="@item.ClientId" maxlength="128" style="width:150px; margin-right:10px;" />
                                        排序：<input type="text" name="btnSortNo" value="@item.SortNo" maxlength="6" style="width:60px; margin-right:10px;" />
                                    </span>
                                    <span class="handle"><a class="del" href="javascript:;">删除筛选条件</a><input type="hidden" name="id" value="" /></span>
                                </dt>
                            </dl>
                        }
                        <div style="margin-left:20px;"><a href="javascript:;" id="addFilter">添加筛选条件</a></div>
                    </div>
                </div>
                <input id="adminMenuId" name="adminMenuId" type="hidden" value="@MenuObj.AdminMenuId" />
                <input id="isParent" name="isParent" type="hidden" value="@MenuObj.IsParent.ToString()" />
                <input id="relevanceUrls" name="relevanceUrls" type="hidden" value="" />
                <div class="easyui-panel" data-options="noheader:true" style="text-align:center">
                    <a id="btn" href="javascript: Submit()" class="easyui-linkbutton" style="width:100px;height:30px" data-options="">提交</a>

                </div>

            </div>

            <div id="fsUploadProgress"> </div>
        </form>

        <script language="javascript" type="text/javascript">
            function resizeForm() {
                //界面变高后,需要重新调整frame的高度,
                try {
                    if (parent)
                        parent.dyniframesize();
                }
                catch (e) { }
            }
            function setValue(type) {
                if (type == 1) {
                    var value = $("#showFlag").val();
                    if (value == 1) {
                        $("#showFlag").val(0);
                        $("#showFlag").removeAttr('checked');
                        $("#showFlagHD").val(0);
                    }
                    else {
                        $("#showFlag").val(1);
                        $("#showFlag").attr('checked', 'checked');
                        $("#showFlagHD").val(1);
                    }
                }
                else if (type == 0) {
                    var value = $("#stopFlag").val();
                    if (value == 1) {
                        $("#stopFlag").val(0);
                        $("#stopFlag").removeAttr('checked');
                        $("#stopFlagHD").val(0);
                    }
                    else {
                        $("#stopFlag").val(1);
                        $("#stopFlag").attr('checked', 'checked');
                        $("#stopFlagHD").val(1);
                    }
                }

            }
            function Submit() {
                $('#editform').form("submit", {
                    url: "DoEdit",
                    onSubmit: function () {
                        var isval = SubmitForm();
                        if (!isval)
                            $.messager.progress('close');
                        return isval;
                    },
                    success: function (data) {
                        $.messager.progress('close');
                        var obj = eval("(" + data + ")");
                        alert(obj.Msg);
                        if (obj.IsSuccess) {
                            //刷新菜单分类
                            //window.parent.frames["leftFrame"].location.reload();
                            window.parent.location.reload();
                        }
                    }
                });
            }
            function AddNextUrl() {
                var emArr = $("[id^='em_']");
                var content = "";
                var id = parseInt($(emArr[emArr.length - 1]).html()) + 1;

                content += "<p id=\"UrlSpan_" + id + "\">菜单关联的路径<em id=\"em_" + id + "\">" + id + "</em>：　Id：<input type=\"text\" id=\"RelevanceUrl_" + id + "\" name=\"RelevanceUrl_" + id + "\" value=\"\" size=\"50\" />";
                content += " <a href=\"javascript:void(0);\" onclick=\"deleteRelevanceUrl(" + id + ");\">删除</a></p>";
                $("#UrlDiv").append(content);
                resizeForm();
            }

            function deleteRelevanceUrl(id) {
                $("#UrlSpan_" + id).remove();
            }

            function SubmitForm() {
                var relevanceUrlArr = $("[id^='RelevanceUrl_']");
                var relevanceUrlss = "";
                for (var i = 0; i < relevanceUrlArr.length; i++) {
                    var _id = $(relevanceUrlArr[i]).val();
                    if (_id != "") {
                        relevanceUrlss += "|&&|" + _id;
                    }
                }
                $("#relevanceUrls").val(relevanceUrlss);

                $(".box").each(function () {
                    var sr = "|&&|";
                    $(this).find("[name='RelUrl']").each(function () {
                        if ($(this).val()) {
                            sr += $(this).val() + "|&&|";
                        }
                    })
                    $(this).find("[name='relList']").val(sr);
                })

                if (!jQuery.trim($("#menuName").val())) {
                    alert("请输入菜单名称！");
                    return false;
                }
                if ($("#urlEorrorMsg:visible").length) {
                    alert("菜单路径无效！");
                    return false;
                }

                return true;
            }
            function DetMenuFun(AdminMenuId, isParent) {
                if (confirm("删除该分类，将删除所有下级分类，所有按钮，已分配的权限等关联数据，确定删除吗?")) {
                    jQuery.ajax({
                        type: "post",
                        url: "doDelete?adminMenuId=" + AdminMenuId,
                        dataType: "json",
                        data: { "adminMenuId": AdminMenuId },
                        success: function (data) {
                            if (data.IsSuccess) {
                                //刷新菜单分类
                                window.parent.location.reload();
                               // window.parent.frames["leftFrame"].location.reload();
                                //刷新当前界面
                                window.location.href = "Edit?action=add&parentId=0&adminMenuId=0";
                                //$("#btn_reload").click();
                            }
                            alert(data.Msg);
                        },
                        error: function () { alert("error"); }
                    });

                }

            }

            $(".showOrHide").toggle(function () {
                $(this).text("展开");
                $(this).parent().parent().next().hide();
            }, function () {
                $(this).text("收起");
                $(this).parent().parent().next().show();
            })

            ////效果JS
            //$(".tip").toggle(function () {
            //    $(this).text("＋");
            //    $(this).parent().next().hide();
            //}, function () {
            //    $(this).text("－");
            //    $(this).parent().next().show();
            //})

            //删除按钮
            $("#btnList .title .del").click(function () {
                var box = $("#btnList").find(".box");
                if (confirm("确认删除？如果是最后一条将清空所填内容！最后不要忘记提交哦！")) {
                    if (box.length <= 1) {
                        box.find("input:text").val("");
                        box.find("[name='id']").val("");
                    }
                    else {
                        $(this).parents(".box").remove();
                    }
                }
            })

            //删除按钮
            $("#filterList .title .del").click(function () {
                var box = $("#filterList").find(".box");
                if (confirm("确认删除？如果是最后一条将清空所填内容！最后不要忘记提交哦！")) {
                    if (box.length <= 1) {
                        box.find("input:text").val("");
                        box.find("[name='id']").val("");
                    }
                    else {
                        $(this).parents(".box").remove();
                    }
                }
            })

            //删除关联url
            $(".content .del").click(function () {
                if ($(this).parent().parent().find(".del").length == 1) {
                    $(this).siblings("input:text").val("");
                }
                else {
                    var box = $(this).parents(".box");
                    $(this).parent().remove();
                    sortNum(box);
                }
            })

            //添加按钮
            $("#addBtn").click(function () {
                var box = $("#btnList .box:last");
                var obj = box.clone(true);
                obj.find("input:text").val(""); //清除复制过来的内容
                obj.find("[name='id']").val("");
                box.after(obj);
                resizeForm();
            })

            //添加条件
            $("#addFilter").click(function () {
                var box = $("#filterList .box:last");
                var obj = box.clone(true);
                obj.find("input:text").val(""); //清除复制过来的内容
                obj.find("[name='id']").val("");
                box.after(obj);
                resizeForm();
            })

            //添加url
            $(".addUrl").click(function () {
                var obj = $(this).parent().prev().clone(true);
                obj.find("input:text").val(""); //清除复制过来的内容
                $(this).parent().before(obj);
                sortNum($(this).parents(".box"));

                resizeForm();
            })

            //生成关联url 序号
            function sortNum(box) {
                var initNum = 1;
                box.find(".content").find("i").each(function () {
                    $(this).text(initNum++);
                })
            }
        </script>

        <script type="text/javascript">
            $("#btnSubmit").click(function () {
                $(".box").each(function () {
                    var sr = "|&&|";
                    $(this).find("[name='RelUrl']").each(function () {
                        if ($(this).val()) {
                            sr += $(this).val() + "|&&|";
                        }
                    })
                    $(this).find("[name='relList']").val(sr);
                })

                if (!jQuery.trim($("#menuName").val())) {
                    alert("请输入菜单名称！");
                    return false;
                }
                if ($("#urlEorrorMsg:visible").length) {
                    alert("菜单路径无效！");
                    return false;
                }
            })

            $("#MenuUrl").blur(function () {
                if (!jQuery.trim($(this).val())) return;
                jQuery.ajax({
                    type: "post",
                    url: "/Admin/AdminMenu/GetRepeatUrlMenu_NP.do?" + Math.random(),
                    dataType: "json",
                    data: { "theurl": escape($(this).val()), "adminMenuId": "@ViewBag.adminMenuId" },
                    success: function (data) {
                        if (data.value != "1") {
                            $("#urlEorrorMsg").show();
                            $("#repeatUrlMenuName").text(data.text);
                        }
                        else {
                            $("#urlEorrorMsg").hide();
                        }
                    },
                    error: function () { alert("error"); }
                })
            })
        </script>
    </div>
</body>
</html>
